﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CoastalClimbing.Code;
using System.Web.Security;

namespace CoastalClimbing.Auth
{
    public partial class Gallery : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                this.BindInfo();
        }

        private void BindInfo()
        {
            try
            {
                grvGallery.DataSource = DAL.GallerySelect();
                grvGallery.DataBind();
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.Message.ToString();
            }
        }

        protected void CancelRecord(object sender, GridViewCancelEditEventArgs e)
        {
            grvGallery.EditIndex = -1;
            this.BindInfo();
        }

        protected void EditRecord(object sender, GridViewEditEventArgs e)
        {
            grvGallery.EditIndex = e.NewEditIndex;
            this.BindInfo();
        }

        protected void UpdateRecord(object sender, GridViewUpdateEventArgs e)
        {
            string id = grvGallery.DataKeys[e.RowIndex].Value.ToString();

            GridViewRow row = grvGallery.Rows[e.RowIndex];
            TextBox txtTitle = (TextBox)row.FindControl("txtTitle");

            try
            {
                if(!string.IsNullOrWhiteSpace(txtTitle.Text))
                {
                    DAL.GalleryEdit(id, txtTitle.Text, Membership.GetUser().ProviderUserKey.ToString());
                    lblMessage.Text = "Record Updated Successfully.";
                }
            }

            catch (Exception ex)
            {
                lblMessage.Text = ex.Message.ToString();
            }

            grvGallery.EditIndex = -1;
            this.BindInfo();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if(!string.IsNullOrWhiteSpace(txtTitle.Text))
            {
                DAL.GalleryCreate(txtTitle.Text, Membership.GetUser().ProviderUserKey.ToString());
                this.BindInfo();

                txtTitle.Text = "";
            }
        }
    }
}